package cn.mengflu.zuul.filter;

import cn.mengflu.zuul.util.FilterUtils;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Mengfly
 * @date 2021/3/17 22:07
 */
@Component
public class JwtTokenFilter extends ZuulFilter {

    private static final int FILTER_ORDER = 1;

//    @Autowired
//    private FilterUtils filterUtils;
//    @Autowired
//    private ServiceConfig serviceConfig;

    @Override
    public String filterType() {
        return FilterUtils.PRE_FILTER_TYPE;
    }

    @Override
    public int filterOrder() {
        return FILTER_ORDER;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        // 实现对Jwt令牌的校验处理
        this.parseJwtToken();
        return null;
    }

    private void parseJwtToken() {
//        if (null == filterUtils.getAuthToken()) {
//            return;
//        }
//        String authToken = filterUtils.getAuthToken().replace("bearer ", "");
//
//        try {
//
//            Claims claims = Jwts.parser()
//                    .setSigningKey(serviceConfig.getJwtSigningKey()
//                            .getBytes("UTF-8")).parseClaimsJws(authToken).getBody();
//            // 对Jwt中包含的信息进行解析，并存放在Zuul过滤器头部中，供后续服务使用
//            filterUtils.setShopId(claims.get("shopId"));
//            filterUtils.setUserId(claims.get("user_name"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }
}
